<template>
  <div style="height: 600px">
    <el-form :model="ruleForm" status-icon :rules="rules" ref="ruleForm" label-width="100px" class="demo-ruleForm" align: center>
      <el-form-item label="原密码" prop="oldpass">
        <el-input type="text" v-model="ruleForm.oldpass" style="width: 300px"></el-input>
      </el-form-item>
      <el-form-item label="新密码" prop="pass">
        <el-input type="text" v-model="ruleForm.pass" autocomplete="off" style="width: 300px"></el-input>
      </el-form-item>
      <el-form-item label="确认密码" prop="checkPass">
        <el-input type="text" v-model="ruleForm.checkPass" autocomplete="off" style="width: 300px"></el-input>
      </el-form-item>
      <div style="margin: 20px 0;  font-size: 24px">
        <el-form-item>
          <el-button type="primary" @click="submitForm('ruleForm')">确认修改</el-button>
        </el-form-item>
      </div>
    </el-form>
  </div>

</template>

<script>
import {IP} from "@/config";
import axios from "axios";
export default {
  name: "ChangePass",
  data() {
    var validatePass0 = (rule, value, callback) => {
      if (value === '') {
        callback(new Error('请输入原密码'));
      } else if (value !== this.password){
        callback(new Error('与原密码不一致，请重新输入！'));
      }else {
        callback();
      }
    };
    var validatePass = (rule, value, callback) => {
      if (value === '') {
        callback(new Error('请输入密码'));
      } else {
        if (this.ruleForm.checkPass !== '' && value !== this.password) {
          this.$refs.ruleForm.validateField('checkPass');
        }else if (value == this.password){
          callback(new Error('与原密码一致,请重新编辑新密码!'));
        }
        callback();
      }
    };
    var validatePass2 = (rule, value, callback) => {
      if (value === '') {
        callback(new Error('请再次输入密码'));
      } else if (value !== this.ruleForm.pass) {
        callback(new Error('两次输入密码不一致!'));
      } else {
        callback();
      }
    };
    return {
      info:{},
      password:'',
      ruleForm: {
        oldpass:'',
        pass: '',
        checkPass: '',
      },
      rules: {
        oldpass: [
          { validator: validatePass0, trigger: 'blur' }
        ],
        pass: [
          { validator: validatePass, trigger: 'blur' }
        ],
        checkPass: [
          { validator: validatePass2, trigger: 'blur' }
        ],

      }
    };
  },
  methods: {
    submitForm(ruleForm) {
      this.$refs[ruleForm].validate((valid) => {
        if (valid){
          this.$confirm('此操作将永久修改密码, 是否继续?', '提示', {
            confirmButtonText: '确定',
            cancelButtonText: '取消',
            type: 'warning'
          }).then(() => {
            let url = IP + "user/edit/edit"
            this.info.password = this.ruleForm.checkPass
            axios.post(url, this.info).then(res => {
              console.log(res)
              if (res.data.code === 0) {
                sessionStorage.setItem("info",JSON.stringify(this.info))
                this.$message({
                  type: 'success',
                  message: res.data.data.status
                });
                this.$router.go(0)
              } else {
                this.$message.error(res.data.data.status);
              }
            }).catch(error => {
              console.log('请求失败')
              console.log(error)
              this.$message.error('请求失败');
            })
          }).catch(() => {
            this.$message({
              type: 'info',
              message: '已取消修改'
            });
          });
        }
        else {
          return this.$message.error('前面有错误！！！')
        }
      });
    },
  },
  mounted() {
    this.info = JSON.parse(sessionStorage.getItem("info"))
    this.password = this.info.password
  }
}

</script>

<style scoped>

</style>